<?php

namespace Biz\Play;

use Base\Exception\NoRecordException;

class Settings extends \Base\BaseBiz
{
    const TYPE_A = 1;
    const TYPE_B = 2;
    const TYPE_C = 3;

    const TYPE_ARR = array(
        self::TYPE_A => '胜平负',
        self::TYPE_B => '半全场',
        self::TYPE_C => '比分'
    );

    //类型标识
    public static function getTypeStr($tag){
        return self::TYPE_ARR[$tag]??'';
    }

    //添加信息
    public static function insert($_orm)
    {
        $rs = array('isok' => false,'msg'=>'');
        try {
            $setting_orm = new \Orm\PlaySettings\PlaySettingsClass();
            $setting_orm['type'] = $_orm['type']??'';
            $setting_orm['title'] = $_orm['title']??'';
            $setting_orm['score_a'] = $_orm['score_a']??'';
            $setting_orm['score_b'] = $_orm['score_b']??'';
            $setting_orm['sort'] = $_orm['sort']??'';
            $setting_orm->insert();
            if($setting_orm['id']){
                $rs['isok'] = true;
            }else{
                throw new \Exception('新增数据失败');
            }
        }catch (\Exception $e) {
            $rs['isok'] = false;
            $rs['msg'] = $e->getMessage();
        }
        return $rs;
    }

    //更新信息
    public static function update($_orm)
    {
        $rs = array('isok' => false,'msg'=>'');
        try {
            $setting_orm = new \Orm\PlaySettings\PlaySettingsClass();
            if(!isset($_orm['id']) || !$_orm['id']){
                throw new \Exception('数据错误');
            }
            $setting_orm['id'] = $_orm['id'];
            $setting_orm['type'] = $_orm['type']??'';
            $setting_orm['title'] = $_orm['title']??'';
            $setting_orm['score_a'] = $_orm['score_a']??'';
            $setting_orm['score_b'] = $_orm['score_b']??'';
            $setting_orm['sort'] = $_orm['sort']??'';
            $is_update = $setting_orm->update();
            if($is_update){
                $rs['isok'] = true;
            }else{
                throw new \Exception('更新失败');
            }
        }catch (\Exception $e) {
            $rs['isok'] = false;
            $rs['msg'] = $e->getMessage();
        }
        return $rs;
    }

    /**
     * 获取信息
     */
    public static function getInfo($id)
    {
        $setting_orm = new \Orm\PlaySettings\PlaySettingsClass();
        $setting_orm->loadById($id);

        return $setting_orm;
    }

    /*
     * 获取列表
     */
    public static function getList($where, $now = 1, $size = 0)
    {
        $now = ($now > 0)?$now:1;
        $size = ($size > 0)?$size:\Util\Config::get('max_limit');

        $setting_list = new \Orm\PlaySettings\PlaySettingsList();
        if (!empty($where)) {
            $setting_list['where'] = $where;
        } else {
            $setting_list['where'] = '';
        }
        $setting_list['limit'] = $size;
        $setting_list['offset'] = ($now - 1) * $size;
        $setting_list['order'] = '`sort` ASC, id ASC';
        $setting_list['key_by'] = 'id';
        $setting_list->load();

        $result = array();
        $result['lists'] = $setting_list['lists'];
        $result['total'] = $setting_list['total'];
        return $result;
    }
}
